//
//  ViewController.m
//  Poem
//
//  Created by xuPeng on 2017/9/27.
//  Copyright © 2017年 xuPeng. All rights reserved.
//

#import "FirstViewController.h"
#import "gagaViewController.h"
#import "Header.h"

@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *dateString;
@property (nonatomic, strong) NSString *timeString;
@property (nonatomic, strong) UILabel *lab1;
@property (nonatomic, strong) UILabel *lab2;
@end

@implementation FirstViewController{
    
        NSTimer *timeNow;
}

- (void)viewWillAppear:(BOOL)animated{
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *str = [UIDevice currentDevice].identifierForVendor.UUIDString;
    [self timerFunc];
    [self creatTableView];
    [self creatNavigationBar];
}

- (void)creatTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (void)creatNavigationBar{
    
    timeNow = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFunc) userInfo:nil repeats:YES];
    
    UIView *timeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    _lab1 = [[UILabel alloc] initWithFrame:CGRectMake(3, 0, 30, 15)];
    _lab2 = [[UILabel alloc] initWithFrame:CGRectMake(3, 15, 30, 15)];
    _lab1.text = _dateString;
    _lab2.text = _timeString;
    _lab1.font = [UIFont systemFontOfSize:8];
    _lab2.font = [UIFont systemFontOfSize:8];
    [timeView addSubview:_lab1];
    [timeView addSubview:_lab2];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:timeView];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.textLabel.text = @"诗";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 1) {
        gagaViewController *gaagVC = [[gagaViewController alloc] init];
        [self.navigationController pushViewController:gaagVC animated:NO];
    }
    
}

//  获取日期和时间

- (void)timerFunc{
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter1 setDateFormat:@"MM.dd"];
    [formatter2 setDateFormat:@"HH:mm"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //----------将nsdate按formatter格式转成nsstring
    _dateString = [formatter1 stringFromDate:datenow];
    _timeString = [formatter2 stringFromDate:datenow];
    
    [_lab1 setText:_dateString];//时间在变化的语句
    [_lab2 setText:_timeString];//时间在变化的语句
    
}
@end
